package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description:
 * @author: xie.zh
 * @create: 2021.03.20   01:07
 * @version: 1.0
 */
@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;

    @GetMapping
    public String toIndex(Model model) {
        List<CategoryEntity> categoryEntities = indexService.queryLevel1Categories();
        model.addAttribute("categories", categoryEntities);
        //TODO：加载广告
        return "index";
    }

    @GetMapping("index.html")
    public String index(Model model) {
        List<CategoryEntity> categoryEntities = indexService.queryLevel1Categories();
        model.addAttribute("categories", categoryEntities);
        //TODO：加载广告
        return "index";
    }

    @ResponseBody
    @GetMapping("index/cates/{pid}")
    public ResponseVo<List<CategoryEntity>> queryLvl2CategoriesWithSub(@PathVariable("pid") Long pid) {
        List<CategoryEntity> categoryEntities = this.indexService.queryLvl2CategoriesWithSub2(pid);
        return ResponseVo.ok(categoryEntities);
    }

    @ResponseBody
    @GetMapping("index/test/lock")
    public ResponseVo testLock() {
        this.indexService.testLock3();
        return ResponseVo.ok();
    }

    @ResponseBody
    @GetMapping("index/test/read")
    public ResponseVo testRead() {
        this.indexService.testRead();
        return ResponseVo.ok();
    }

    @ResponseBody
    @GetMapping("index/test/write")
    public ResponseVo testWrite() {
        this.indexService.testWrite();
        return ResponseVo.ok();
    }

    @ResponseBody
    @GetMapping("index/test/latch")
    public ResponseVo testLatch() {
        this.indexService.testLatch();
        return ResponseVo.ok("班长锁门了");
    }

    @ResponseBody
    @GetMapping("index/test/countDown")
    public ResponseVo testcountDown() {
        this.indexService.testcountDown();
        return ResponseVo.ok("出来了一位同学");
    }
}